package com.ysd.modules.sys.controller;

import com.ysd.common.utils.PageUtils;
import com.ysd.common.utils.R;
import com.ysd.modules.sys.entity.SysClassesEntity;
import com.ysd.modules.sys.entity.SysStudent;
import com.ysd.modules.sys.entity.SysTeacher;
import com.ysd.modules.sys.entity.SysTeacherVO;
import com.ysd.modules.sys.service.SysClassesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(description = "PC-班级管理+学生管理+老师管理")
@RequestMapping("/sys/classes")
public class SysClassesController {

    @Autowired
    private SysClassesService classesService;

    
    @ApiOperation("通过学校Id获取所属班级")
    @GetMapping("getClassesBySchoolId")
    public R getClassesBySchool(int schoolId) {
       
    	List<SysClassesEntity> list=classesService.GetBySchoolId(schoolId);
        return R.ok().put("data", list);
    }
    
    
    @ApiOperation("根据班级名分页查询班级")
    @GetMapping("getClasses")
    public R getClasses(String cname, Integer page, Integer limit) {
        PageUtils allClasses = classesService.getAllClasses(cname, page, limit);
        return R.ok().put("data", allClasses);
    }
    
    @ApiOperation("根据班级名分页查询班级")
    @GetMapping("getClassByParams")
    public R getClassByParams(Integer sid,String cname,String jy,String fdy,Integer page, Integer limit) {
    	Map<String, Object> params = new HashMap<String, Object>();
    	params.put("sid", sid);
    	params.put("cname", cname);
    	params.put("jy", jy);
    	params.put("fdy", fdy);
    	params.put("page", page);
    	params.put("limit", limit);
    	System.out.println(params);
		PageUtils allClasses = classesService.getClassByParams(params);
		return R.ok().put("data", allClasses);
	}
    
    @PostMapping("addClasses")
    @ApiOperation("添加班级")
    public R addClasses(@RequestBody SysClassesEntity c) {
        boolean flag = classesService.save(c);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @PostMapping("delClasses")
    @ApiOperation("根据id删除班级")
    public R delClasses(Integer cid) {
        int i = classesService.delClasses(cid);
        if (i > 0) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @PostMapping("updateClasses")
    @ApiOperation("修改班级")
    public R updateClasses(@RequestBody SysClassesEntity c) {
        int i = classesService.updateClasses(c);
        if (i > 0) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation("根据学生名分页查询学生")
    @GetMapping("getAllStudents")
    public R getAllStudents(String cname, Integer page, Integer limit) {
        PageUtils pageUtils = classesService.getAllStudents(cname, page, limit);
        return R.ok().put("data", pageUtils);
    }

    @PostMapping("addStudents")
    @ApiOperation("添加学生")
    public R addStudents(@RequestBody SysStudent sysStudent) {
        int i = classesService.addStudents(sysStudent);
        if (i > 0) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @PostMapping("delStudents")
    @ApiOperation("根据id删除学生")
    public R delStudents(Integer id) {
        int i = classesService.delStudents(id);
        if (i > 0) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @PostMapping("updateStudents")
    @ApiOperation("修改学生")
    public R updateStudents(SysStudent c) {
        int i = classesService.updateStudents(c);
        if (i > 0) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @PostMapping("addTeacher")
    @ApiOperation("添加老师")
    public R addTeacher(@RequestBody SysTeacher sysTeacher) {
        int i = classesService.addTeacher(sysTeacher);
        if (i > 0) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @PostMapping("delTeacher")
    @ApiOperation("根据id删除老师")
    public R delTeacher(Integer id) {
        int i = classesService.delTeacher(id);
        if (i > 0) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @PostMapping("updateTeacher")
    @ApiOperation("修改老师")
    public R updateTeacher(SysTeacher c) {
        int i = classesService.updateTeacher(c);
        if (i > 0) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation("根据老师名查询老师")
    @GetMapping("getAllTeacher")
    public R getAllTeacher(String searchName) {
        List<SysTeacherVO> sysTeacherVOS = classesService.getAllTeacher(searchName);
        return R.ok().put("data", sysTeacherVOS);
    }
    
    @ApiOperation("查询所有老师")
    @GetMapping("getTeacherAll")
    public R getTeacherAll() {
        List<SysTeacher> sysTeacherVOS = classesService.getTeacherAll();
        return R.ok().put("data", sysTeacherVOS);
    }
}
